package com.neusoft.reportserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.reportserver.dto.ReportSearch;
import com.neusoft.reportserver.dto.RespBean;
import com.neusoft.reportserver.entity.Report;
import com.neusoft.reportserver.mapper.ReportMapper;
import com.neusoft.reportserver.service.IReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 销售日报表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {
    @Autowired
    ReportMapper reportMapper;

    @Override
    public RespBean addReport(Report report) {
        RespBean respBean = RespBean.ok(200, "Report added successfully!");
        reportMapper.insert(report);
        return respBean;
    }

    @Override
    public RespBean deleteReport(int id) {
        RespBean respBean = RespBean.ok(200, "Report deleted successfully!");
        reportMapper.deleteById(id);
        return respBean;
    }

    @Override
    public RespBean updateReport(Report report) {
        RespBean respBean = RespBean.ok(200, "Report updated successfully!");
        reportMapper.updateById(report);
        return respBean;
    }

    @Override
    public RespBean searchReport(ReportSearch reportSearch) {
        LambdaQueryWrapper<Report> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(reportSearch.getCustomerMgrId() != null)
            lambdaQueryWrapper.eq(Report::getCustomerMgrId, reportSearch.getCustomerMgrId());
        if(reportSearch.getMarketDept() != null && reportSearch.getMarketDept().length() > 0)
            lambdaQueryWrapper.eq(Report::getReportDept, reportSearch.getMarketDept());
        if(reportSearch.getWeek() != null && reportSearch.getWeek().length() > 0)
            lambdaQueryWrapper.eq(Report::getReportWeek,reportSearch.getWeek());
        if(reportSearch.getDateTo() != null && reportSearch.getDateTo().length() > 0){
            System.out.println(reportSearch.getDateTo());
            lambdaQueryWrapper.le(Report::getReportDate, reportSearch.getDateTo());
        }
        if(reportSearch.getDateFrom() != null && reportSearch.getDateFrom().length() > 0){
            System.out.println(reportSearch.getDateFrom());
            lambdaQueryWrapper.ge(Report::getReportDate, reportSearch.getDateFrom());
        }
        List<Report> reports = this.list(lambdaQueryWrapper);
        RespBean respBean = RespBean.ok(200, "Find " + reports.size() + " report!");
        respBean.setObj(reports);
        return respBean;
    }
}
